package com.learning.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.learning.dto.PermissionDTO;
import com.learning.entity.Permission;
import com.learning.mapper.PermissionMapper;
import com.learning.result.Result;
import com.learning.service.IPermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.learning.vo.PermissionVO;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 教师权限表 服务实现类
 * </p>
 *
 * @author itRao
 * @since 2024-05-23
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements IPermissionService {

    /**
     * 根据教师ID查询教师权限
     *
     * @param teacherId
     * @return
     */
    @Override
    public Result<PermissionVO> queryByTeacherId(Long teacherId) {
        Permission permission = lambdaQuery().eq(Permission::getTeacherId, teacherId).one();

        PermissionVO vo = BeanUtil.copyProperties(permission, PermissionVO.class);
        return Result.success(vo);
    }

    /**
     * 修改教师权限
     *
     * @param permissionDTO
     */
    @Override
    public void updatePermission(PermissionDTO permissionDTO) {
        Permission oldPermission = lambdaQuery().eq(Permission::getTeacherId, permissionDTO.getTeacherId()).one();

        Long id = oldPermission.getId();

        Permission newPermission = BeanUtil.copyProperties(permissionDTO, Permission.class);

        newPermission.setId(id);

        updateById(newPermission);
    }
}
